﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;
using System.Web;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Html
{
    public class Element : IDesignerInitialization
    {
        [Property]
        public string Tag { get; set; }
        [Property]
        public string Text { get; set; }
        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            return "<" + this.Tag + ">" + HttpContext.Current.Server.HtmlEncode(this.Text) + "</" + this.Tag + ">";
        }

        public string Name
        {
            get { return "element"; }
        }

        public string Namespace
        {
            get { return "html";  }
        }

        public string[] Parameters
        {
            get { return new string[] {};  }
        }

        public void Design(IDesigner designer)
        {
            var t = this.Text;
            if (string.IsNullOrEmpty(t))
                t = "<i>" + this.Tag + "</i>";

            designer.AddControl("Html:Element:" + this.Tag)
                .AddContentBeforeChildren("<" + this.Tag + ">" + t)
                .AddContentAfterChildren("</" + this.Tag + ">");
        }

        public string AddToTemplate(Xml.IXmlElement input, string initializer)
        {
            return ControlInitializerHelper.AddToTemplate("<html:element tag=\"span\"/>", input, initializer);
        }

        public string[] GetInitializers()
        {
            return new string[] {"Insert Before", "Insert After"};
        }
    }
}
